package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import object.Category;
import object.ReturnBool;
import tpInterface.ICategory;

/**
 *
 * @author SonTT
 */
public class CategoryM implements ICategory {

    private TPConnection connection;

    public CategoryM() {
    }

    public static Iterator getCategories(String test) {
        String driveString = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://localhost:1433; databasename=tech-pro";
        String username = "sa";
        String password = "fat123456";

        Vector<Category> categories = new Vector<Category>();

        try {
            Class.forName(driveString);
            Connection connection = DriverManager.getConnection(url, username, password);
            Statement stm = connection.createStatement();
            String query = "SELECT * FROM Category";
            ResultSet rs = stm.executeQuery(query);

            if (rs.wasNull()) {
                return null;
            } else {
                while (rs.next()) {
                    Category c = new Category(
                            rs.getInt(1),
                            rs.getNString(2),
                            rs.getNString(3),
                            rs.getDate(4));
                    categories.addElement(c);
                }
                return categories.iterator();
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryM.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(ProductM.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    @Override
    public List<Category> getCategoies() {
        List<Category> categories = new ArrayList<Category>();

        try {
            connection = new TPConnection();
            Statement stm = connection.getConnection().createStatement();
            String query = "SELECT * FROM Category";
            ResultSet rs = stm.executeQuery(query);

            if (rs.wasNull()) {
                return null;
            } else {
                while (rs.next()) {
                    Category c = new Category(
                            rs.getInt(1),
                            rs.getNString(2),
                            rs.getNString(3),
                            rs.getDate(4));
                    categories.add(c);
                }

                return categories;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryM.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(CategoryM.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    @Override
    public ReturnBool addCategory(Category category) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ReturnBool editCategory(Category category) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ReturnBool deleteCategory(String categoryID) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ReturnBool deleteCategories(String[] categoryIDs) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Category> searchCategory(String keyword) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    private static final Logger LOG = Logger.getLogger(CategoryM.class.getName());
}
